package com.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ems.entity.TOrder;
import com.ems.mapper.TOrderMapper;
import com.ems.service.ITOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements ITOrderService {


    @Override
    public boolean dispatch(TOrder tOrder) {
        baseMapper.dispatch(tOrder);
        return true;
    }

    @Override
    public boolean confirm(TOrder tOrder) {
        baseMapper.confirm(tOrder);
        return true;
    }

    @Override
    public List<TOrder> listDispatch() {
        List<TOrder> list = baseMapper.listDispatch();
        return list;
    }

    @Override
    public boolean finish(TOrder tOrder) {
        baseMapper.finish(tOrder);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> detail() {
        TOrder order =new TOrder();
        QueryWrapper<TOrder> query = new QueryWrapper<TOrder>();
        query.eq("del_flag",0);
        query.select("IFNULL(sum(number),0) as planNumber");
        QueryWrapper<TOrder> query1 = new QueryWrapper<TOrder>();
        query1.eq("del_flag",0);
        query1.select("IFNULL(sum(finish_number),0) as realNumber");
        Map<String, Object> map = this.getMap(query);
        map.put("realNumber",this.getMap(query1).get("realNumber"));
        //完成率
        QueryWrapper<TOrder> query2 = new QueryWrapper<TOrder>();
        query2.select("IFNULL(count(number),0) as totalNumber").eq("del_flag",0);
        QueryWrapper<TOrder> query3 = new QueryWrapper<TOrder>();
        query3.select("IFNULL(count(number),0) as finishNumber").eq("finish_id",1).eq("del_flag",0);
        float comrates =new BigDecimal(this.getMap(query3).get("finishNumber").toString()).divide(new BigDecimal(this.getMap(query2).get("totalNumber").toString()),2,BigDecimal.ROUND_HALF_UP).floatValue();
        map.put("comrates",comrates);
        //未派功率
        QueryWrapper<TOrder> query4 = new QueryWrapper<TOrder>();
        query4.select("IFNULL(count(id),0) as totalOrd").eq("del_flag",0);
        QueryWrapper<TOrder> query5 = new QueryWrapper<TOrder>();
        query5.select("IFNULL(count(id),0) as dispatchOrd").eq("mac_id",null).eq("del_flag",0);
        float unDispatch =new BigDecimal(this.getMap(query5).get("dispatchOrd").toString()).divide(new BigDecimal(this.getMap(query4).get("totalOrd").toString()),2,BigDecimal.ROUND_HALF_UP).floatValue();
        map.put("unDispatch",unDispatch);
        return map;
    }

    @Override
    public List<TOrder> getByOrderNumber(Integer orderId) {
        List<TOrder> order = baseMapper.getByOrderNumber(orderId);
        return order;
    }
}
